package com.ybwh.springboot2.common;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Collection;
import java.util.List;

/**
 * 分页模型
 *
 * @author fanbeibei
 * @date 2020/10/2 15:43
 */
@Data
public class Pagination<T> {
    @ApiModelProperty(value = "当前页号,从1开始")
    private int pageNum;
    @ApiModelProperty(value = "每页的数量")
    private int pageSize;
    @ApiModelProperty(value = "总页数")
    private int pages;
    @ApiModelProperty(value = "总记录数")
    protected long total;
    @ApiModelProperty(value = "结果集")
    protected List<T> list;


    /**
     * 包装Page对象
     *
     * @param page page结果
     */
    public Pagination(Page<T> page) {
        this.pageNum = page.getPageNum();
        this.pageSize = page.getPageSize();
        this.pages = page.getPages();
        this.total = page.getTotal();
        this.list = page.getResult();
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
